<?php

// +----------------------------------------------------------------------
// | 文件 UserMsgShowEvent.class.php
// +----------------------------------------------------------------------
// | 说明 对用户的提示消息做的缓存策略
// +----------------------------------------------------------------------
// | 
// +----------------------------------------------------------------------
// | Author: 谢赤心 Legend. <xcx_legender@qq.com>
// +----------------------------------------------------------------------


namespace Api\Event\Cache;
use Api\Event\CacheEvent;

class UserMsgShowEvent implements ICacheEvent
{	
	/**
	hash   
	{
		'mail' => 1,    // 站内信消息
		'msg'  => 0,    // 动态消息
	}


	class::get($uid, $field)
	class::set($uid, $field)
	class::setAll($uid)
	class::reset($uid, $field)
	* -class::del($uid)-

	 */
	static protected $DB = 1;
	const PREFIX = 'IMBA.USER.HASMSG.';
	static $field_map = ['mail', 'msg'];

	public function set($uid, $field)
	{
		$init = false;
		$redis = CacheEvent::instance(self::DB);
		$key = self::PREFIX.$uid;
		if (!$redis->exists($key)){
			$init = true;
		}

		if (!in_array($field, self::$field_map)){
			return false;
		}

		switch ($field) {
			case 'mail':
				$value = event('Mail')->isNew($uid);
				break;
			case 'msg':
				$value = event('MessageE')->getNewCount($uid);
				break;
			default:
				return false;
				break;
		}

		$value = (int)$value;
		$redis->hset($key, $field, $value);
		return $value;
	}

	public function get($uid, $field = '')
	{
		if ($field && !in_array($field, self::$field_map)){
			return null;
		}
		$redis = CacheEvent::instance(self::DB);
		$key = self::PREFIX.$uid;
		if ($field){
			return $redis->hget($key, $field);
		}else{
			return $redis->hgetall($key);
		}
	}

}